import { $get, $put } from '@js/request'
import { InputBatchSchema } from '@components/schema/inputBatch'
import { CaseSchema } from '@components/schema/caseSchema'
import { ConditionSchema } from '@components/schema/condition'

// 获取导入信息
export async function getInfoRequest (id: string): Promise<InputBatchSchema> {
  const res = await $get('/api/common/input_excel', {
    condition: JSON.stringify([{ k: 'id', o: '=', v: id }]),
    page: 1,
    limit: 1
  })
  const list: InputBatchSchema[] = res.data.list
  if (list.length === 0) {
    throw new Error('获取导入信息失败')
  }
  return list[0]
}

export interface ErrorSchema { fail_type: string, c: number }

// 获取案件列表
export async function getListRequest (id: string, d: { condition: ConditionSchema[], page: number, limit: number }): Promise<{ list: CaseSchema[], total: number, success_count: number, failinfo: Array<{ fail_type: string, c: number }> }> {
  const res = await $get('/api/common/input_excel/case_temp_list', {
    condition: JSON.stringify(d.condition),
    import_logid: id,
    page: d.page,
    limit: d.limit
  })
  const list: CaseSchema[] = res.data.list instanceof Array ? res.data.list : []
  list.forEach((li, i) => {
    li.orderno = (d.page - 1) * d.limit + i + 1
  })
  return res.data
}

// 重新导入
export async function importAgainRequest (id: string): Promise<void> {
  const res = await $put('/api/common/input_excel/case_temp_import_fail', {
    import_logid: id
  })
  return res.data
}
